﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using EmotionChat.Services;
using Mvvm.Models;
using Mvvm.Services;
using ReactiveUI;

namespace Mvvm.ViewModels;

public class DirectoryViewModel : PageViewModelBase
{
    private readonly IChatDataProcessedStorage _chatDataProcessedStorage;
    private readonly IChatTimeAIAdviceService _chatTimeAIAdviceService;
    private readonly IChatTimeReportService _chatTimeReportService;
    private readonly IChatDataStorage _chatDataStorage;

    public DirectoryViewModel(IChatDataProcessedStorage chatDataProcessedStorage, 
        IChatTimeAIAdviceService chatTimeAIAdviceService, 
        IChatTimeReportService chatTimeReportService,
        IChatDataStorage chatDataStorage)
    {
        _chatDataProcessedStorage = chatDataProcessedStorage;
        _chatTimeAIAdviceService = chatTimeAIAdviceService;
        _chatTimeReportService = chatTimeReportService;
        _chatDataStorage = chatDataStorage;
        Init();
        _createTimeReportCommand = new AsyncRelayCommand(CreateTimeReport);
        _aiAdviceCommand = new AsyncRelayCommand(AIAdvice);
    }
    
    private int _identifier;

    public int Identifier
    {
        get => _identifier;
        set => this.RaiseAndSetIfChanged(ref _identifier, value);
    }

    private string _myName;

    public string MyName
    {
        get => _myName;
        set { this.RaiseAndSetIfChanged(ref _myName, value); }
    }

    private string _onesName;

    public string OnesName
    {
        get => _onesName;
        set { this.RaiseAndSetIfChanged(ref _onesName, value); }
    }

    private string _timeReport;

    public string TimeReport
    {
        get => _timeReport;
        set { this.RaiseAndSetIfChanged(ref _timeReport, value); }
    }
    
    private ChatTimeAIAdvice _aiAdvice;

    public ChatTimeAIAdvice AiAdvice
    {
        get => _aiAdvice;
        set { this.RaiseAndSetIfChanged(ref _aiAdvice, value); }
    }
    
    private bool _isLoading1;

    public bool IsLoading1 {
        get => _isLoading1;
        set => this.RaiseAndSetIfChanged(ref _isLoading1, value);
    }
    
    private bool _isLoading2;

    public bool IsLoading2 {
        get => _isLoading2;
        set => this.RaiseAndSetIfChanged(ref _isLoading2, value);
    }
    
    private ICommand _createTimeReportCommand { get; }

    public async Task CreateTimeReport()
    {
        IsLoading1 = true;
        List<ChatProcessedData> chatProcessDataList = await _chatDataProcessedStorage.QueryAsync(_myName, _onesName);
        TimeReport = _chatTimeReportService.createReport(chatProcessDataList);
        IsLoading1 = false;
    }
    private ICommand _aiAdviceCommand { get; }

    public async Task AIAdvice()
    {
        IsLoading2 = true;
        List<ChatProcessedData> chatProcessDataList = await _chatDataProcessedStorage.QueryAsync(_myName, _onesName);
        Identifier = chatProcessDataList[0].Identifier;
        List<ChatData> chatDataList = await _chatDataStorage.QueryAsync(Identifier);
        string chatData =  _chatTimeAIAdviceService.GetChatData(chatDataList);
        var _content = 
            new Content() { role = "user", content = "根据聊天记录：\n" + chatData + "提出对以后聊天的建议，比如如何回复的建议或关于聊天时间的建议等等，并且字数不超过800字，不要标题，段落间不要有空行，尽量少分段落。"};
        var task =  await _chatTimeAIAdviceService.CreateChatTimeAIAdvice(_content);
        AiAdvice = new ChatTimeAIAdvice(task);
        IsLoading2 = false;
    }
    
    public async Task Init()
    {
        IsLoading1 = false;
        IsLoading2 = false;
        chatProcessedDatas = await _chatDataProcessedStorage.QueryAsync(1);
        _myName = chatProcessedDatas[0].Username1;
        _onesName = chatProcessedDatas[0].Username2;
      
    }
    public static List<ChatProcessedData> chatProcessedDatas { set; get; }
   
    public override bool CanNavigateNext 
    { 
        get => true;
        protected set => throw new NotSupportedException(); 
    }
    
    public override bool CanNavigatePrevious
    {
        get => true;
        protected set => throw new NotSupportedException();
    } 
}